﻿using CommandLine;
using PCore = PBIW.Core.Core;
using SysConsole = System.Console;

namespace PBIW.Console.Commands
{
    public static class ConfigCommand
    {
        [Verb("config", HelpText = "调整配置。")]
        public sealed class Options : IOption
        {
            [Option("reference", HelpText = "启用或关闭《周易》及其象传彖传支持。")]
            public bool? ZhouyiReferencing { get; set; } = null;

            [Option('c', "autocalculation", HelpText = "启用或关闭自动运算。")]
            public bool? EnableAutoCalculation { get; set; } = null;
        }

        public sealed class Executor : IExecutor<Options>
        {
            private readonly PCore core;
            public Executor(PCore core)
            {
                this.core = core;
            }

            public void Execute(Options options)
            {
                bool withOptions = false;
                if (options.ZhouyiReferencing.HasValue)
                {
                    withOptions = true;
                    if (options.ZhouyiReferencing.Value)
                    {
                        var s = this.core.TryEnableAndReloadZhouyiReferencing();
                        SysConsole.WriteLine(s ?
                            "成功启用《周易》及其象传彖传支持。" :
                            "《周易》及其象传彖传支持启用失败。请检查文件。");
                    }
                    else
                    {
                        this.core.DisableZhouyiReferencing();
                        SysConsole.WriteLine("成功关闭《周易》及其象传彖传支持。");
                    }
                }
                if (options.EnableAutoCalculation.HasValue)
                {
                    withOptions = true;
                    var boolValue = options.EnableAutoCalculation.Value;
                    this.core.EnableAutoCalculation = boolValue;
                    SysConsole.WriteLine(
                        boolValue ?
                        "成功启用自动运算。" :
                        "成功关闭自动运算。");
                }
                SysConsole.WriteLine(withOptions ?
                    "配置修改完毕。" :
                    "没有发现任何修改项。如果你想获取关于 config 指令的帮助，请在指令后添加 --help 。");
            }
        }
    }
}
